package org.git.movie.videojiexi.service.impl;

import com.google.protobuf.compiler.PluginProtos;
import org.jsoup.Jsoup;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DayuHaoServiceImpl {
    /**
     * 大鱼号分享视频地址为
     *
     * https://mparticle.uc.cn/video.html?uc_param_str=frdnsnpfvecpntnwprdsssnikt&btifl=10016&app=uc-iflow&title_type=1&wm_id=62d33c39d287466396421f5b08b1e395&wm_cid=366123656790675456&pagetype=share&client=&uc_share_depth=1
     * 解析出的视频地址为：
     * https://dy-frontend.video.ums.uc.cn/video/wemedia/62d33c39d287466396421f5b08b1e395/9068b0fb24e11126f2e990f0d76a84b8-3567799990-2-0-3-h264.mp4?auth_key=1598240510-895fcc90eb434e23915ac613b1ae4604-0-da3c4bb89412538fb3276faa3803039e
     *
     * 重点事件：点击播放按钮后才出现视频地址。
     */
    public static void main(String[] args) throws IOException {
        String playUrl = "https://dy-frontend.video.ums.uc.cn/video/wemedia/62d33c39d287466396421f5b08b1e395/9068b0fb24e11126f2e990f0d76a84b8-3567799990-2-0-3-h264.mp4?auth_key=1598247317-390c76511ee145fe95195ae648e8dd5a-0-7340e0a4e5b78e3ec87cbea75ba8cd46";
        BufferedInputStream inStream = null;


//:authority: dy-frontend.video.ums.uc.cn
//:method: GET
//        :path: /video/wemedia/62d33c39d287466396421f5b08b1e395/9068b0fb24e11126f2e990f0d76a84b8-3567799990-2-0-3-h264.mp4?auth_key=1598247317-390c76511ee145fe95195ae648e8dd5a-0-7340e0a4e5b78e3ec87cbea75ba8cd46
//        :scheme: https
//        accept: */*
//accept-encoding: identity;q=1, *;q=0
//accept-language: zh-CN,zh;q=0.9
//cookie: cna=rNGjF95UIG8CAd8UKDq9e8en
//range: bytes=0-
//referer: https://mparticle.uc.cn/video.html?uc_param_str=frdnsnpfvecpntnwprdsssnikt&btifl=10016&app=uc-iflow&title_type=1&wm_id=62d33c39d287466396421f5b08b1e395&wm_cid=366123656790675456&pagetype=share&client=&uc_share_depth=1
//sec-fetch-dest: video
//sec-fetch-mode: no-cors
//sec-fetch-site: same-site
//user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36
        Map<String,String> headers = new HashMap();
//        headers.put(":authority","dy-frontend.video.ums.uc.cn");
       // headers.put(":method","GET");
//        headers.put(":path","/video/wemedia/62d33c39d287466396421f5b08b1e395/9068b0fb24e11126f2e990f0d76a84b8-3567799990-2-0-3-h264.mp4?auth_key=1598247317-390c76511ee145fe95195ae648e8dd5a-0-7340e0a4e5b78e3ec87cbea75ba8cd46");
        //headers.put(":scheme","https");
        headers.put("cookie","cna=rNGjF95UIG8CAd8UKDq9e8en");
        headers.put("range","bytes=0-");
        headers.put("referer","https://mparticle.uc.cn/video.html?uc_param_str=frdnsnpfvecpntnwprdsssnikt&btifl=10016&app=uc-iflow&title_type=1&wm_id=62d33c39d287466396421f5b08b1e395&wm_cid=366123656790675456&pagetype=share&client=&uc_share_depth=1");
        headers.put("sec-fetch-dest","video");
        headers.put("sec-fetch-mode","no-cors");
        headers.put("sec-fetch-site","same-site");
        inStream = Jsoup.connect(playUrl).timeout(10000).ignoreContentType(true).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36")
                .headers(headers)
                .execute().bodyStream();

        Date now = new Date();
        String   filename = now.getTime()+".mp4";

        saveFile(inStream,"D:\\1.mp4");//保存文件的地址
        //FileOutputStream fileOutputStream = new   FileOutputStream(fileName);
    }
    /**
     * 保存文件到本地
     * @param bufferedInputStream
     * @param savePath
     */
    public static void saveFile(BufferedInputStream bufferedInputStream,String savePath) throws IOException {
        //一次最多读取1k
        byte[] buffer = new byte[1024];
        //实际读取的长度
        int readLenghth;
        //根据文件保存地址，创建文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath));
        //创建的一个写出的缓冲流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        //文件逐步写入本地
        while ((readLenghth = bufferedInputStream.read(buffer,0,1024)) != -1){//先读出来，保存在buffer数组中
            System.out.println(readLenghth);
            bufferedOutputStream.write(buffer,0,readLenghth);//再从buffer中取出来保存到本地
        }
        //关闭缓冲流
        bufferedOutputStream.close();
        fileOutputStream.close();
        bufferedInputStream.close();
    }

}
